package edu.ccut.saturn.studio.database.adapters;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.studio.database.model.DBConnection;

/**
 * 默认实现
 * 
 * @author 谷钰
 * @version 1.0 2008.12.02
 * 
 */
public class BaseDatatbaseAdapter implements IDatabaseAdapter {

	public String filterTableName(String schemaName, String tableName) {
		return tableName;
	}

	public ResultSet getColumns(DatabaseMetaData metaData, String catalog,
			String schemaPattern, String tableNamePattern,
			String columnNamePattern) throws SQLException {

		return metaData.getColumns(catalog, schemaPattern, tableNamePattern,
				columnNamePattern);
	}

	public ResultSet getPrimerKey(DatabaseMetaData metaData, String catalog,
			String schema, String table) throws SQLException {

		return metaData.getPrimaryKeys(catalog, schema, table);
	}

	public List<String> getSchemas(DatabaseMetaData metaData,
			ResultSet schemaRts, List<String> schemaNames) throws Exception {
		schemaRts = metaData.getSchemas();
		while (schemaRts.next()) {
			schemaNames.add(schemaRts.getString("TABLE_SCHEM"));
		}
		return schemaNames;
	}

	public List<String> getTables(DatabaseMetaData metaData, String catalog,
			String schemaPattern, String tableNamePattern, String type,
			ResultSet resultSet) throws SQLException {
		List<String> names = new ArrayList<String>();

		resultSet = metaData.getTables("", schemaPattern.toUpperCase(), "%",
				new String[] { type });
		while (resultSet.next()) {
			names.add(resultSet.getString(3));
		}
		return names;
	}

	public String getDatabaseName(DBConnection connection) {
		return connection.getUsername();
	}
}
